package com.example.sunmiv1.adapter;

import android.content.Context;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.StoreRetreatOrderBean;
import com.example.sunmiv1.utils.net.ACache;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PurchaseRefundAdapter extends RecyclerView.Adapter<PurchaseRefundAdapter.prViewHolder> {

    private Context mContext;
    private List<StoreRetreatOrderBean.DataBean> mList;
    private ItemDetailClickListener mItemDetailClickListener;
    private String pricePermission;// 門店查看價格權限

    public PurchaseRefundAdapter(Context context, List<StoreRetreatOrderBean.DataBean> list) {
        super();
        mContext = context;
        mList = list;
        ACache aCache = ACache.get(context);
        pricePermission=aCache.getAsString("showPrice");
    }

    @Override
    public prViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_purchase_refund, parent, false);
        return new prViewHolder(view);
    }

    @Override
    public void onBindViewHolder(prViewHolder holder, int position) {
//        final StoreRetreatOrderBean.DataBean dataBean=mList.get(position);
        holder.infoOperator.setText(mList.get(position).getOperator());
        holder.infoRefundTime.setText(mList.get(position).getAdd_time());
        holder.infoOrderNumber.setText(mList.get(position).getOrder_number());
        holder.infoSupplier.setText(mList.get(position).getSupplier());
        holder.infoWarehouse.setText(mList.get(position).getWarehouse());
        holder.infoTotalKind.setText(mList.get(position).getGoods_species());
        holder.infoPurchasedPrice.setText(mList.get(position).getTotal_cost_price() + "");
        holder.infoRefund.setText(mList.get(position).getMoney_total() + "");
        holder.infoDifferenceMoney.setText(mList.get(position).getDifference_money() + "");
//        holder.tvDetail.setText(mList.get(position).getOperator());
        holder.tvDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mItemDetailClickListener.onItemDetailClick(position);
            }
        });
        if (!TextUtils.isEmpty(mList.get(position).getRemarks())) {
            holder.groupRemark.setVisibility(View.VISIBLE);
            holder.infoRemark.setText(String.valueOf(mList.get(position).getRemarks()));
        } else {
            holder.groupRemark.setVisibility(View.GONE);
        }

        if("2".equals(pricePermission)){
            ((View)holder.infoPurchasedPrice.getParent()).setVisibility(View.GONE);
            ((View)holder.infoRefund.getParent()).setVisibility(View.GONE);
            ((View)holder.infoDifferenceMoney.getParent()).setVisibility(View.GONE);
        }
    }

    @Override
    public void onViewDetachedFromWindow(prViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }


    static class prViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.info_operator)
        TextView infoOperator;
        @BindView(R.id.info_refund_time)
        TextView infoRefundTime;
        @BindView(R.id.info_order_number)
        TextView infoOrderNumber;
        @BindView(R.id.info_supplier)
        TextView infoSupplier;
        @BindView(R.id.info_warehouse)
        TextView infoWarehouse;
        @BindView(R.id.info_total_kind)
        TextView infoTotalKind;
        @BindView(R.id.info_purchased_price)
        TextView infoPurchasedPrice;
        @BindView(R.id.info_refund)
        TextView infoRefund;
        @BindView(R.id.info_difference_money)
        TextView infoDifferenceMoney;
        @BindView(R.id.info_remark)
        TextView infoRemark;
        @BindView(R.id.group_remark)
        RelativeLayout groupRemark;
        @BindView(R.id.tv_detail)
        TextView tvDetail;

        public prViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public static interface ItemDetailClickListener {
        void onItemDetailClick(int position);
    }

    public void setItemDetailClickListener(ItemDetailClickListener itemDetailClickListener) {
        mItemDetailClickListener = itemDetailClickListener;
    }

}
